package org.tbjd.client.guis.taskList.items;

import java.util.ArrayList;
import java.util.Date;

import org.tbjd.client.beans.Task;
import org.tbjd.client.customWidgets.taskList.TreeGridItemHandle;

import com.google.gwt.user.client.ui.Widget;

public class TreeGridItem {

	public static final String ITEM_SELECTED = "itemSelected";
	public static final String ITEM_NOT_SELECTED = "itemNotSelected";
	
	/* Widgets to display in the column of the TreeGrid */
	private ArrayList<Widget> widgets = new ArrayList<Widget>();

	/* Children of this TreeGridItem */
	private ArrayList<TreeGridItem> children = null;

	private TreeGridItem parent = null;
	
	private long id = new Date().getTime();

	private Task bean = null;

	private int position = -1;

	public TreeGridItem(){
	}
	
	
	public TreeGridItem(ArrayList<Widget> lstValues, Task data){
		this.widgets = lstValues;
		this.bean = data;
	}
	
	/**
	 * Add another TreeGridItem
	 */
	public void addItem(TreeGridItem treeGridItem) {

		if (this.children == null) {
			initChildren();
		}

		children.add(treeGridItem);
		treeGridItem.setParent(this);

	}

	public ArrayList<Widget> getWidgets() {
		return widgets;
	}

	/**
	 * Init the children of this TreeGridItem
	 */
	private void initChildren() {
		children = new ArrayList<TreeGridItem>();
	}

	public void setValues(ArrayList<Widget> lstWidgets) {
		widgets = lstWidgets;
	}

	public ArrayList<Widget> getValues() {
		//TODO doublon avec getWidets() ???
		return widgets;
	}

	public boolean hasChildren() {
		return (children != null && children.size() > 0);
	}

	public ArrayList<TreeGridItem> getChildren() {
		return children;
	}

	public void setChildren(ArrayList<TreeGridItem> children) {
		this.children = children;
	}

	public TreeGridItem getParent() {
		return parent;
	}

	public void setParent(TreeGridItem parent) {
		this.parent = parent;
	}

	public Task getBean() {
		return bean;
	}

	public void setBean(Task bean) {
		this.bean = bean;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}
	
	/**
	 * Set the CSS styleName of its widgets
	 * @param styleName
	 */
	public void setStyleName(String styleName){
		for (Widget eachWidget : this.getWidgets()) {
			eachWidget.setStyleName(styleName);
		}
	}

	public long getId() {
		return id;
	}


	public void setId(long id) {
		this.id = id;
	}
	
	public String toString() {
		return "TreeGridItem.toString() - id = " + getId() + ", name=" + getBean().getName();
	}
	
	public TreeGridItemHandle getHandel(){
		return (TreeGridItemHandle) widgets.get(0);
	}
	
}
